Put the ipynb file and html file in the github branch you created in the last assignment and submit the link to the commit in brightspace
from plotly.offline import init_notebook_mode
import plotly.io as pio
import plotly.express as px
init_notebook_mode(connected=True)
pio.renderers.default = "plotly_mimetype+notebook"
#load data
df = px.data.gapminder()
df.head()
| country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
|---|---|---|---|---|---|---|---|---|
| 0 | Afghanistan | Asia | 1952 | 28.801 | 8425333 | 779.445314 | AFG | 4 |
| 1 | Afghanistan | Asia | 1957 | 30.332 | 9240934 | 820.853030 | AFG | 4 |
| 2 | Afghanistan | Asia | 1962 | 31.997 | 10267083 | 853.100710 | AFG | 4 |
| 3 | Afghanistan | Asia | 1967 | 34.020 | 11537966 | 836.197138 | AFG | 4 |
| 4 | Afghanistan | Asia | 1972 | 36.088 | 13079460 | 739.981106 | AFG | 4 |
Recreate the barplot below that shows the population of different continents for the year 2007.
Hints:
df_2007 = df.query('year==2007')
df_new = df_2007.groupby("continent").sum()
fig = px.bar(
df_new, x= "pop", y = df_new.index, orientation = 'h',
color= df_new.index
)
fig.show()
# YOUR CODE HERE
df_2007 = df.query('year==2007')
df_new = df_2007.groupby("continent").sum()
fig = px.bar(
df_new, x= "pop", y = df_new.index, orientation = 'h',
color= df_new.index
)
fig.update_yaxes(categoryorder="total ascending")
fig.show()
Add text to each bar that represents the population
# YOUR CODE HERE
df_2007 = df.query('year==2007')
df_new = df_2007.groupby("continent").sum()
fig = px.bar(
df_new, x= "pop", y = df_new.index, orientation = 'h',
color= df_new.index, text_auto=".2s"
)
fig.update_traces(textfont_size=12, textangle=0, textposition="outside", cliponaxis=False)
fig.update_yaxes(categoryorder="total ascending")
fig.show()
Thus far we looked at data from one year (2007). Lets create an animation to see the population growth of the continents through the years
# YOUR CODE HERE
df = px.data.gapminder()
df_new = df.groupby(['continent', 'year'], as_index = False)['pop'].sum()
print(df_new)
fig = px.bar(df_new, x= "pop", y = "continent", animation_frame="year", animation_group= "continent",
color="continent",
range_x=[0,4000000000])
fig.update_yaxes(categoryorder="total ascending")
fig.show()
continent year pop 0 Africa 1952 237640501 1 Africa 1957 264837738 2 Africa 1962 296516865 3 Africa 1967 335289489 4 Africa 1972 379879541 5 Africa 1977 433061021 6 Africa 1982 499348587 7 Africa 1987 574834110 8 Africa 1992 659081517 9 Africa 1997 743832984 10 Africa 2002 833723916 11 Africa 2007 929539692 12 Americas 1952 345152446 13 Americas 1957 386953916 14 Americas 1962 433270254 15 Americas 1967 480746623 16 Americas 1972 529384210 17 Americas 1977 578067699 18 Americas 1982 630290920 19 Americas 1987 682753971 20 Americas 1992 739274104 21 Americas 1997 796900410 22 Americas 2002 849772762 23 Americas 2007 898871184 24 Asia 1952 1395357351 25 Asia 1957 1562780599 26 Asia 1962 1696357182 27 Asia 1967 1905662900 28 Asia 1972 2150972248 29 Asia 1977 2384513556 30 Asia 1982 2610135582 31 Asia 1987 2871220762 32 Asia 1992 3133292191 33 Asia 1997 3383285500 34 Asia 2002 3601802203 35 Asia 2007 3811953827 36 Europe 1952 418120846 37 Europe 1957 437890351 38 Europe 1962 460355155 39 Europe 1967 481178958 40 Europe 1972 500635059 41 Europe 1977 517164531 42 Europe 1982 531266901 43 Europe 1987 543094160 44 Europe 1992 558142797 45 Europe 1997 568944148 46 Europe 2002 578223869 47 Europe 2007 586098529 48 Oceania 1952 10686006 49 Oceania 1957 11941976 50 Oceania 1962 13283518 51 Oceania 1967 14600414 52 Oceania 1972 16106100 53 Oceania 1977 17239000 54 Oceania 1982 18394850 55 Oceania 1987 19574415 56 Oceania 1992 20919651 57 Oceania 1997 22241430 58 Oceania 2002 23454829 59 Oceania 2007 24549947
Instead of the continents, lets look at individual countries. Create an animation that shows the population growth of the countries through the years
df = px.data.gapminder()
fig = px.bar(
df, x= "pop", y = "country", orientation = 'h', animation_frame="year", animation_group= "country", hover_name="country",
color= "country", range_x=[0, 1400000000]
)
fig.update_yaxes(categoryorder="total ascending")
fig.show()
Clean up the country animation. Set the height size of the figure to 1000 to have a better view of the animation
df = px.data.gapminder()
fig = px.bar(
df, x= "pop", y = "country", orientation = 'h', animation_frame="year", animation_group= "country", hover_name="country",
color= "country", range_x=[0, 1400000000], height=1000
)
fig.update_yaxes(categoryorder="total ascending")
fig.show()
# YOUR CODE HERE
import plotly.graph_objects as go
df = px.data.gapminder()
n = len(df.country.unique())
fig = px.bar(
df, x= "pop", y = "country", orientation = 'h', animation_frame="year", animation_group= "country", hover_name="country",
color= "country", range_x=[0, 1400000000], range_y=[n-10, n]
)
fig.update_yaxes(categoryorder="total ascending")
fig.show()